home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9718 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  56 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.Stanford.EDU!microunity!toms
  3. From: toms@MicroUnity.com (Tom Sanders)
  4. Subject: Re: while loop problem
  5. Message-ID: <Do6DB0.EtE@microunity.com>
  6. Sender: usenet@microunity.com (news id)
  7. Organization: MicroUnity Systems Engineering, Inc.
  8. References:  <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca>
  9. Date: Tue, 12 Mar 1996 22:04:12 GMT
  10.  
  11. In article <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca>, Bill Simpson <wsimpson@uwinnipeg.ca> writes:
  12. |> Consider the following code fragment:
  13. |> 
  14. |> y=2000; z=1000;
  15. |> x=0;
  16. |> while(x<=XMAX)
  17. |>     {
  18. |>     x+=(int) erand(mean);
  19. |>     setdot(x,y,z);
  20. |>     }
  21. |>     
  22. |> It plots a line of dots that are randomly (exponentially) spaced, giving
  23. |> a 1D spatial Poisson process.
  24. |> The problem is that dot x coordinates can only be between 0 and XMAX.
  25. |> The above code will also attempt to plot one point at an x value >XMAX
  26. |> before it terminates.
  27. |> 
  28. |> Is there a nice way to write the code so it works properly?
  29. |> 
  30. |> The only way I have thought of is
  31. |> y=2000; z=1000;
  32. |> x=0;
  33. |> while(x<=XMAX)
  34. |>     {
  35. |>     x+=(int) erand(mean);
  36. |>     if(x<=XMAX)
  37. |>         setdot(x,y,z);
  38. |>     }
  39. |> 
  40. |> which seems very clumsy since the same test is done twice.
  41. |> 
  42. |> Thanks very much for any help.
  43. |> 
  44. |> Bill Simpson
  45.  
  46. How about:
  47.  
  48. y=2000; z=1000;
  49. x=0;
  50. do  {
  51.      x+=(int) erand(mean);
  52.      setdot(x,y,z);
  53.      } while(x<=XMAX)
  54.  
  55. Tom Sanders
  56.